#!/bin/bash
#
# Small script to view images from a  remote dir.
# 20090830:jpa:orig
#

pic_delay=${1:-3600}
reload_delay=${2:-600}
# Leave empty for default.
picasaweb_album_name="${3:-OLPC XO PictureFrame2}"
max_pics=${4:-400}

download_dir=$HOME/FramePics
view_cmd="feh --recursive --hide-pointer --full-screen --auto-zoom"
fetch_pic_count=0
fetch_res=1
fehpid=

# This is where the sw lives, a clone of the git repo would work just fine.
picture_frame_sw_dir=$(cd -P ${0%/*}; pwd)
export PYTHONPATH=$picture_frame_sw_dir/gdata-2.0.17/src
# The following var is also extracted from album comment.
album_info="$picasaweb_album_name"
picasaweb_cmd="$picture_frame_sw_dir/picasaweb.py --imgmax_size=1200"


function GetConfig() {
  local cfg=${1:-$download_dir}/slideshow.config
  if [ -e $cfg ]; then
    eval $(egrep '^(reload_delay|pic_delay|album_info)=.*$' $cfg)
  fi
}

function FetchPictures() {
  local out_dir="$download_dir.new"
  rm -rf "$out_dir"
  mkdir -p "$out_dir"

  wget -q --tries=1 --timeout=5 --directory-prefix="$out_dir" \
    --no-host-directories \
    http://User:@192.168.1.10/IMAGE.JPG

  gst-launch v4l2src num-buffers=1 ! ffmpegcolorspace ! jpegenc ! filesink location="$out_dir/LOCALCAM.JPG"

  $picasaweb_cmd --photo_limit="$max_pics" --out_dir="$out_dir" ${picasaweb_album_name:+--album_name="$picasaweb_album_name"}
  fetch_res=$?

  fetch_pic_count=$(find "$out_dir" -type f ! \( -name slideshow.config -o -name IMAGE.JPG -o -name LOCALCAM.JPG \) | wc -l)
  fetch_pic_count=$((fetch_pic_count))
  echo "picasaweb.py: res=$fetch_res files=$fetch_pic_count"
}

function StartView() {
  [ -n "$fehpid" ] && kill $fehpid
  $view_cmd --slideshow-delay=$pic_delay --info "echo '$album_info'" "$download_dir" &
  fehpid=$!
  trap "echo killing fehpid=$fehpid ; kill $fehpid" EXIT
}

function WaitForInitialFetch() {
  [ -d "$download_dir" ] && return
  FetchPictures
  UpdateFromNewDir
  while [ ! -d "$download_dir" ]; do
    sleep 60
    FetchPictures
    UpdateFromNewDir
  done
}

function UpdateFromNewDir() {
  local diff_res
  local got_new_dir=0

  diff -s --exclude=LOCALCAM.JPG --exclude=IMAGE.JPG -r "$download_dir.new" "$download_dir"
  diff_res=$?

  if [ $fetch_res -eq 0 -a $fetch_pic_count -ne 0 -a $diff_res -ne 0 ]; then
    # Maybe the dir doesn't exist on 1st run.
    [ -d "$download_dir" ] && mv "$download_dir" "$download_dir.todelete"
    mv "$download_dir.new" "$download_dir"
    got_new_dir=1
  else
    mv "$download_dir.new" "$download_dir.todelete"
  fi
  rm -rf "$download_dir.todelete"
  (( got_new_dir ))
}

function MainLoop() {
  local update_res

  # Disable power management's energystar features.
  xset -dpms

  while true; do
    date

    FetchPictures
    UpdateFromNewDir
    update_res=$?

    if [ $update_res -eq 0 ]; then
      GetConfig
      StartView
    fi

    # Delay could be pic_count based.
    sleep $((reload_delay + pic_count * pic_delay / 100 ))
  done

}

set -vx
GetConfig
WaitForInitialFetch
StartView
MainLoop
